package org.jeecg.common.util.result;

/**
 * @author guoyh
 * @ClassName ResultMapper
 * @Description
 * @since 2020/8/11 22:51
 **/
public class ResultMapper {
    /**
     * Wrap.
     *
     * @param <E>     the element type
     * @param code    the code
     * @param message the message
     * @param o       the o
     * @return the wrapper
     */
    public static <E> Result<E> wrap(int code, String message, E o) {
        return new Result<E>(code, message, o);
    }

    /**
     * Wrap.
     *
     * @param <E>     the element type
     * @param code    the code
     * @param message the message
     * @return the wrapper
     */
    public static <E> Result<E> wrap(int code, String message) {
        return wrap(code, message, null);
    }

    /**
     * Wrap.
     *
     * @param <E>  the element type
     * @param code the code
     * @return the wrapper
     */
    public static <E> Result<E> wrap(int code) {
        return wrap(code, null);
    }

    /**
     * Wrap.
     *
     * @param <E> the element type
     * @param e   the e
     * @return the wrapper
     */
    public static <E> Result<E> wrap(Exception e) {
        return new Result<E>(Result.ERROR_CODE, e.getMessage());
    }

    /**
     * Un wrapper.
     *
     * @param <E>     the element type
     * @param wrapper the wrapper
     * @return the e
     */
    public static <E> E unWrap(Result<E> wrapper) {
        return wrapper.getData();
    }

    /**
     * Wrap ILLEGAL_ARGUMENT. code=100
     *
     * @param <E> the element type
     * @return the wrapper
     */
    public static <E> Result<E> illegalArgument() {
        return wrap(Result.ILLEGAL_ARGUMENT_CODE, Result.ILLEGAL_ARGUMENT_MESSAGE);
    }

    /**
     * Wrap TIME_OUT. code=110
     *
     * @param <E> the element type
     * @return the wrapper
     */
    public static <E> Result<E> timeOut() {
        return wrap(Result.TIME_OUT_CODE, Result.TIME_OUT_MESSAGE);
    }

    /**
     * Wrap SECURE_FAIL. code=120
     *
     * @param <E> the element type
     * @return the wrapper
     */
    public static <E> Result<E> secureFail() {
        return wrap(Result.SECURE_FAIL_CODE, Result.SECURE_FAIL_MESSAGE);
    }

    /**
     * Wrap SUCCESS. code=200
     *
     * @param <E> the element type
     * @return the wrapper
     */
    public static <E> Result<E> ok() {
        return new Result<E>();
    }

    /**
     * Wrap ERROR. code=500
     *
     * @param <E> the element type
     * @return the wrapper
     */
    public static <E> Result<E> error() {
        return wrap(Result.ERROR_CODE, Result.ERROR_MESSAGE);
    }
}
